﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BulkUploadGovernanceFiles.Models;
using MongoDB.Driver;
using MongoDB.Driver.Builders;

namespace BulkUploadGovernanceFiles.Repositories
{
    public class InsightRepository
    {
        private readonly MongoCollection<Insight> _governanceInsight;

        public InsightRepository()
        {
            var corporateDataConnectionString = ConfigManager.ClientPlatform.ConnectionString;

            var mongoClient = new MongoClient(corporateDataConnectionString);
            var server = mongoClient.GetServer();
            var database = server.GetDatabase(ConfigManager.ClientPlatform.DataBaseName);

            _governanceInsight = database.GetCollection<Insight>("Insight");   
        }

        public List<Insight> FindAll()
        {
            return _governanceInsight.FindAll().ToList();
        }

        public void Update(Guid id, string fieldName, string fieldValue)
        {
            var query = Query.EQ("_id", id);
            var sortBy = SortBy.Null;
            var update = new UpdateBuilder().Set(fieldName, fieldValue);
            _governanceInsight.FindAndModify(query, sortBy, update);
        }
    }
}
